﻿
using Acme.BookStore.Books;
using Acme.BookStore.Bookss;
using Acme.BookStore.UserInfors;
using Acme.BookStore.UserInforss;
using BWShop.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore
{
    public class UserInforAppService : ApplicationService, IUserInforService
    {
        private readonly IRepository<UserInfor, int> _userInfors;
        public UserInforAppService(IRepository<UserInfor, int> userInfors)
        {
            _userInfors = userInfors;
        }
        //验证码
        [HttpGet]
        public CheckCodeDto CheckCode()
        {
            /*var code = ValidateCodeHelper.CreateRandomCode(4);
            //把生成的随机数,存到session
            HttpContext.Session.SetString("code", code);

            var b = ValidateCodeHelper.CreateValidateGraphic(code);
            string str = Convert.ToBase64String(b);
            return Ok("data:image/jpeg;base64," + str);*/


            var snofk = new IdWorker(1, 1);
            string id = snofk.NextId().ToString();
            var code = ValidateCodeHelper.CreateRandomCode(4);
            var b = ValidateCodeHelper.CreateValidateGraphic(code);
            string str = Convert.ToBase64String(b);
            RedisHelper redis = new RedisHelper();
            redis.Set("code" + id, code);

            return new CheckCodeDto { code = "data:image/jpeg;base64," + str, idWord = id };
        }

        //注册
        [HttpGet]
        public int CreateUserinfor(CreateUpdateUserInforDto createUpdate)
        {
            var user = new UserInfor()
            {
                usersName = createUpdate.usersName,
                usersPassword = createUpdate.usersPassword,
                usersPhone = createUpdate.usersPhone
            };
            _userInfors.InsertAsync(user);
            return 10;
        }
        //登录
        [HttpGet]
        public int GetUserInfor(UserInforqureyDto dto)
        {
            var list = from a in _userInfors
                       select new UserInforqureyDto
                       {
                           usersName = a.usersName,
                           usersPassword = a.usersPassword,
                       };
            var name = list.WhereIf(!string.IsNullOrEmpty(dto.usersName), s => s.usersName.Equals(dto.usersName)).FirstOrDefault();
            if (name == null)
            {
                return 10;
            }
            var password = list.WhereIf(!string.IsNullOrEmpty(dto.usersPassword), s => s.usersPassword.Equals(dto.usersPassword)).FirstOrDefault();
            if (password == null)
            {
                return 20;
            }
            return 5;
        }

    }
}

